package api

import (
	"api-gateway/basic/inits"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"api-gateway/untils"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

// ListOneDayData
// @Summary      查看某天数据
// @Description  查看某天数据
// @Tags         数据模块
// @Accept       json
// @Produce      json
// @Param        dataReporting  query      request.OneDayDataReq  true  "健康数据"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /dataReporting/oneDayData [get]
func ListOneDayData(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.OneDayDataReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.UserCline.GetOneDaySummarize(c, &__.GetOneDaySummarizeReq{
		UserId:      int64(userId),
		EquipmentId: req.EquipmentId,
		Date:        req.Date,
	})
	if err != nil {
		response.Error(c, 500, "服务端错误", err.Error())
		return
	}
	timeGet := time.Now().AddDate(0, 0, -1).Format(time.DateOnly)
	if len(list.List) == 0 {
		response.Success(c, "无周期内数据", nil)
		return
	}
	data := fmt.Sprintf("%v日数据汇总", timeGet)
	untils.CreateExcel(list.List, data)
	response.Success(c, "查询成功", list)
}

// HealthDataAdd
// @Summary      用户手动添加数据
// @Description  用户手动添加数据
// @Tags         数据模块
// @Accept       json
// @Produce      json
// @Param        healthData  body     request.HealthDataAdd  true  "数据数据"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /healthData/healthDataAdd [post]
func HealthDataAdd(c *gin.Context) {
	var req request.HealthDataAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	created, err := inits.UserCline.UserHealthDataCreated(context.Background(), &__.UserHealthDataCreatedReq{
		UserId:          req.UserId,
		EquipmentId:     req.EquipmentId,
		BodyTemperature: req.BodyTemperature,
		BloodSugar:      req.BloodSugar,
		BloodPressure:   req.BloodPressure,
		BloodPressureD:  req.BloodPressureD,
		BloodOxygen:     req.BloodOxygen,
	})
	if err != nil {
		response.Error(c, 500, "用户手动添加数据失败", err.Error())
		return
	}
	response.Success(c, "用户手动添加数据添加成功", created.Id)
}

// HealthDataList
// @Summary      用户查看数据
// @Description  用户查看数据
// @Tags         数据模块
// @Accept       json
// @Produce      json
// @Param        healthData  query     request.HealthDataList  true  "数据数据"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /healthData/healthDataList [get]
func HealthDataList(c *gin.Context) {
	var req request.HealthDataList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	data, err := inits.UserCline.GetUserHealthData(context.Background(), &__.GetUserHealthDataReq{
		UserId:   req.UserId,
		Page:     req.Page,
		PageSize: req.PageSize,
	})
	if err != nil {
		response.Error(c, 500, "用户查找健康信息失败", err.Error())
		return
	}
	response.Success(c, "用户查找健康信息成功", data)

}
